package bancosys.tec.security.impl.diagnostician;

import static bancosys.tec.rtm.diagnostician.AbstractDiagnostician.JMX_DEFAULT_CACHE_DURATION_IN_SECONDS;
import static bancosys.tec.rtm.diagnostician.AbstractDiagnostician.JMX_NAMESPACE;
import static bancosys.tec.security.impl.diagnostician.UsersLoggedDiagnostician.NAME;

import org.springframework.jmx.export.annotation.ManagedAttribute;
import org.springframework.jmx.export.annotation.ManagedResource;

import bancosys.tec.rtm.diagnostician.AbstractDiagnostician;
import bancosys.tec.rtm.diagnostician.DiagnosisBean;
import bancosys.tec.security.impl.UsersLoggedCounter;

/**
 * Diagnosticador que indica quantos usuarios estao logados nesse momento no sistema.
 * 
 * @author Marcio Ribeiro (mmr)
 * @created Dec 9, 2011
 */
@ManagedResource(description = NAME, objectName = JMX_NAMESPACE + ":name=usersLogged", currencyTimeLimit = JMX_DEFAULT_CACHE_DURATION_IN_SECONDS)
public class UsersLoggedDiagnostician extends AbstractDiagnostician {
    public static final String NAME = "Usuários logados";

    private static final String KEY_USERS = "users";

    private UsersLoggedCounter counter;

    /**
     * {@inheritDoc}
     */
    public String getName() {
        return NAME;
    }

    /**
     * @return contador.
     */
    public UsersLoggedCounter getCounter() {
        return counter;
    }

    /**
     * Define contador.
     * 
     * @param counter contador.
     */
    public void setCounter(UsersLoggedCounter counter) {
        this.counter = counter;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    protected boolean fillDiagnosisBean(DiagnosisBean bean) throws Exception {
        int n = counter.getUsers();
        bean.addDescription(n + " usuários estão logados nesse momento.");
        bean.putValue(KEY_USERS, n);
        return true;
    }

    /**
     * @return numero de usuarios logados nesse momento.
     */
    @ManagedAttribute(description = KEY_USERS)
    public Integer getUsers() {
        return (Integer) getValue(KEY_USERS);
    }
}
